
def get_pagination(count, limit, p):
    pages = []
    pages_max = 9
    pages_half = 4
    page_numb = 0
    # calc page numb
    if count % limit == 0:
        page_numb = count / limit
    else:
        page_numb = int(count / limit) + 1
    # process prev and next
    if p <= 1:
        prev = -1
    else:
        prev = p - 1
    if p >= page_numb:
        next = -1
    else:
        next = p + 1
    # generate pages
    if p <= pages_half+1:
        end = pages_max
        if page_numb < pages_max : end = page_numb
        for n in range(1, end+1): pages.append(n)
    elif p >=page_numb-pages_half:
        start = 1
        if page_numb-pages_max+1 > 1: start = page_numb-pages_max+1
        for n in range(start, page_numb+1): pages.append(n)
    else:
        for n in range(-pages_half, pages_half+1): pages.append(p+n)

    if len(pages)>0 and prev==-1: prev=pages[0]
    if len(pages)>0 and next==-1: next=pages[-1]

    return dict(prev=prev, pages=pages, next=next)